package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.VCheckReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 个人测评结果管理控制类
 * @author zl
 * @since 2018/11/7 19:24
 */

@Controller
@RequestMapping(value="/sysperevaluation")
public class SysPerEvaluationResult {
	
	/**
	 * 个人心理测评查询与分析
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 个人心理测评查询与分析
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "个人心理测评查询与分析", 
			operationClassPath = "controller.SysTitleManagementController.getPersonalEvaluationSelect(Model model, HttpServletRequest req,HttpServletResponse resp)", 
			operationMenu = "个人测评结果管理/个人心理测评查询与分析")
	@RequestMapping(value="/getperevaselect")
	public String getPersonalEvaluationSelect(Model model,HttpServletRequest req,HttpServletResponse resp) throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result=new JSONObject();
		Expression exp=new Expression();
		
		String checktime=req.getParameter("checktime");
		String reporttype=req.getParameter("reporttype");
		String classid=req.getParameter("classid");
		String stuid=req.getParameter("stuid");
		String currentPage =req.getParameter("page");
		String pageSize = req.getParameter("limit");
		
		String schoolid = req.getParameter("schoolid");
		
		try {
			if(currentPage!=null && pageSize!=null){
				
				if(schoolid!=null && !schoolid.equals("")){
					exp.andEqu("schoolid",schoolid,String.class); //添加schoolid的查询条件
				}
				if(reporttype!=null && !reporttype.equals("")){
					exp.andLike("checkname", reporttype, String.class);
				}
				if(classid!=null && !classid.equals("")){
					exp.andEqu("classid",classid,String.class); //添加classid的查询条件
				}
				if(stuid!=null && !stuid.equals("")){
					exp.andLeftBraLike("username", stuid, String.class); //添加username的查询条件
					exp.orRightBraLike("realName", stuid, String.class);
				}
				if(checktime!=null && !checktime.equals("")){
					String[] checktimes=checktime.split("/");
					String starttime=checktimes[0];
					String endtime=checktimes[1];
					exp.andBetween("checktime", starttime, String.class);
					exp.andAnd(endtime, String.class);	
				}
				exp.orderByAsc("schooltype");
				exp.orderByAsc("schoolid");
				exp.orderByAsc("grade");
				exp.orderByAsc("classid");
				exp.orderByAsc("realname");
				String strwhere=exp.toString();
				List<VCheckReport > listsize= DAOFactory.getCheckReportDAO().getReportByCondition(strwhere);
				List<VCheckReport > reportList = DAOFactory.getCheckReportDAO().getReportByCondition(strwhere,
						Integer.parseInt(currentPage),Integer.parseInt(pageSize));
				if(reportList!=null && reportList.size()>0){
					result.put("code",0);
					result.put("msg", "查询成功");
					result.put("count", listsize.size());
					JSONArray jsonArray = JSONArray.fromObject(reportList);
					result.element("data", jsonArray);
				}else{
					result.put("code", 1);
					result.put("msg", "无查询结果");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "无查询结果");
		}
		
		PrintWriter write=resp.getWriter();
		write.print(result);
		return null;
	}
	
	
}